package com.dog.basics.rabbitmq.service;

import lombok.SneakyThrows;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import com.rabbitmq.client.Channel;
import java.util.List;
import java.util.Map;

/**
 * @program: d-basic-composition
 *
 * @description: 测试MQ
 *
 * @author: Guo
 *
 * @create: 2019-08-14 12:09
 **/
public class TestRabbitMqService {

	//监听消息队列 - 普通队列(配置)
/*	@RabbitListener(
			bindings = @QueueBinding(
					exchange = @Exchange(value = "EX-TEST",arguments={@Argument(name = "alternate-exchange",value = "exchange-unroute")}),
					value = @Queue(value = "QU-TEST",arguments={@Argument(name = "x-dead-letter-exchange",value = "OVERTIME-TEST")}),
					key = "GUT")
	)*/
	@SneakyThrows
	public void consumeMessage(@Payload List<String> objects, Channel channel, @Headers Map<String,Object> headers) {
		objects.forEach(e->{
			System.out.println("consumeMessage-GUT is Ok send :"+e);
		});
		long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
		channel.basicAck(deliveryTag,false);
		//channel.basicNack(deliveryTag,false,false);
	}

	//监听消息队列  -  死信(配置 - 死信无KEY)
/*	@RabbitListener(
			bindings = @QueueBinding(
					exchange = @Exchange(value = "OVERTIME-TEST",type = ExchangeTypes.FANOUT ),
					value = @Queue(value = "QU-OVERTIME-TEST"))
	)*/
	@SneakyThrows
	public void consumeMessageA(@Payload List<String> objects, Channel channel, @Headers Map<String,Object> headers) {
		objects.forEach(e->{
			System.out.println("consumeMessage-TIME is Ok send :"+e);
		});
		long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
		channel.basicAck(deliveryTag,false);
		//channel.basicNack(deliveryTag,false,false);
	}

}
